package com.ljcalc.business.components;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class LJCalcProperties {

	private static LJCalcProperties instance;
    private static final String DEFAULT_PROPERTY_FILE_NAME = "ljcalc.properties";
    private Properties properties = new Properties();
	
	private LJCalcProperties(String filename){
		if(filename==null){
			filename = DEFAULT_PROPERTY_FILE_NAME;
		}
        //Suppose it is in the classpath, use the current classloader
        ClassLoader cls = getClass().getClassLoader();
        // XXX propertyFile is not set here which prevents storing later on.
        InputStream is = cls.getResourceAsStream(filename);
        try {
        	//if we dont find any file, don't load the properties
        	if(is!=null){
        		properties.load(is);
        	}
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}
	
	public static LJCalcProperties getInstance(){
		if(instance==null){
			instance = new LJCalcProperties(null);
		}
		return instance;
	}
	
	public static LJCalcProperties getInstance(String filename){
		if(instance==null){
			instance = new LJCalcProperties(filename);
		}
		return instance;
	}
	
    public String getProperty(String key) {
        return properties.getProperty(key);
    }

    public void setProperty(String key, String value) {
        this.properties.setProperty(key, value);
    }
}
